package com.xuqm.sdhbwfu.app.ui

import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.viewModels
import com.cfca.mobile.cmbc.log.MLog
import com.xuqm.base.dialog.loading.LoadingDialog
import com.xuqm.base.extensions.getStringForPreferences
import com.xuqm.base.extensions.loge
import com.xuqm.base.extensions.putString
import com.xuqm.base.extensions.showMessage
import com.xuqm.base.ui.BaseActivity
import com.xuqm.sdhbwfu.app.R
import com.xuqm.sdhbwfu.app.databinding.ActivityLoginPswBinding
import com.xuqm.sdhbwfu.app.viewmodel.LoginViewModel
import com.xuqm.sdhbwfu.core.common.SHARE_UESR_NAME
import com.xuqm.sdhbwfu.core.common.SHARE_UESR_PASSWORD
import com.xuqm.sdhbwfu.core.common.SHARE_UESR_TOKEN

class LoginPswActivity : BaseActivity<ActivityLoginPswBinding>() {
    override fun getLayoutId() = R.layout.activity_login_psw

    val RSA_PUBLIC_KEY =
        "30818902818100AC681DBF90C8C2DE37EECB315B43058A6A5C7DCF40E21AE492DCC48C1AD3506EC07F71DB23074D3B8F44139D731A79874B82F6BCB4D7CD1D9AE21921B156759A4D37467E54B5CC60818A5F65F9567FBFC2AACB1FD5F36CE8CF2217FAAEF41550F355B7B9D99874CBEA525C223C5F0A4D2736E83514301D96E5A9B7C912CB65E30203010001"
    val RSA_PUBLIC_KEY_SIG =
        "65D8C3F3396A4871EE969900FBE0697C413F0B47A2F76AA3873D3942682F7C01582F853589AD557BECEAC7D6B0FADD07005CB17A563A438902D1E7E70BDB4927B9A5FAFF50B42C00183CC3100E486B40409911A253571B067BF27687943361C01A9F6CAB6480D65D5302FD3A4AF5E919D576CDCFCAD1486A24F65179B32EA845"

    val SR64 = "s404040404040404"

    private val vm: LoginViewModel by viewModels()
    override fun fullscreen(): Boolean =true
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        binding.loginUserEdit.setText(getStringForPreferences(SHARE_UESR_NAME))

        binding.loginPwdEdit.setEncrypt(true)
        binding.loginPwdEdit.setButtonPress(true)
        binding.loginPwdEdit.setShowPassword(false)
        binding.loginPwdEdit.setWatchOutside(true)


        binding.loginPwdEdit.setEncrypt(true)
        binding.loginPwdEdit.setButtonPress(true)
        binding.loginPwdEdit.setShowPassword(false)
        binding.loginPwdEdit.setWatchOutside(true)
        binding.loginPwdEdit.isSymbolKeyboardDisplay = true
        binding.loginPwdEdit.isBlankSpace = true
        binding.loginPwdEdit.isSupportExternalKeyboard = false

        binding.loginPwdEdit.setMaxLength(100)
        binding.loginPwdEdit.setInputRegex(null)
        binding.loginPwdEdit.setMatchRegex(null)

        binding.loginPwdEdit.setPublicKey(0, RSA_PUBLIC_KEY, RSA_PUBLIC_KEY_SIG)
        // guardEdit.setPublicKey(RSA_PUBLIC_KEY); //微通
        binding.loginPwdEdit.setCipherKey(SR64)
        binding.loginPwdEdit.setServerRandom(SR64)
        MLog.setMLogClose(false)
        binding.loginPwdEdit.initPassGuardKeyBoard()


        binding.loginPwdEdit.setKeyBoardShowAction {
        }
        binding.loginPwdEdit.setKeyBoardHideAction { }


//        val psw = getStringForPreferences(SHARE_UESR_PASSWORD)
//        if (psw.isNotEmpty()) {
//            binding.loginPwdEdit.setText(psw)
//            binding.checkBox.isChecked = true
//        } else
//            binding.checkBox.isChecked = false


        binding.checkBox.setOnCheckedChangeListener { _, isChecked -> isChecked.loge() }
        binding.loginConfirmLyt.setOnClickListener {
            binding.checkBox.isChecked = !binding.checkBox.isChecked
        }

    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        binding.loginPwdEdit.resetKeyboardIfConfigurationChanged()
    }
    override fun initData() {
        super.initData()
        vm.status.observe(this) {
            LoadingDialog.dismissDialog()
            if (null != it) {
                putString(SHARE_UESR_TOKEN, it)
                startActivity(Intent(mContext, MainActivity::class.java))
                finish()
            }

        }
    }
    private fun getWindowHeight(): Int {
        val dm = resources.displayMetrics
        return dm.heightPixels
    }
    override fun lateInitView() {
        super.lateInitView()

        binding.loginConfirmTv.setOnClickListener {

            val username = binding.loginUserEdit.text.toString()
            if (username.isEmpty()) {
                "请输入用户名".showMessage()
                return@setOnClickListener
            }
            val psw = binding.loginPwdEdit.output1
            if (binding.loginPwdEdit.output3 <= 0 || psw.isNullOrEmpty()) {
                "请输入密码".showMessage()
                return@setOnClickListener
            }
            if (binding.checkBox.isChecked) {
                putString(SHARE_UESR_PASSWORD, psw)
            } else {
                putString(SHARE_UESR_PASSWORD, "")
            }
            putString(SHARE_UESR_NAME, username)
            LoadingDialog.showDialog("")
            vm.login(username, psw)
        }
    }

}